<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Coroutines  Reference</title>
    <link rel="stylesheet" type="text/css" href="css/jazzy.css" />
    <link rel="stylesheet" type="text/css" href="css/highlight.css" />
    <meta charset='utf-8'>
    <script src="js/jquery.min.js" defer></script>
    <script src="js/jazzy.js" defer></script>
    
  </head>
  <body>
    <a title="Coroutines  Reference"></a>
    <header>
      <div class="content-wrapper">
        <p><a href="index.html">Venice Docs</a> (100% documented)</p>
        <p class="header-right"><a href="https://github.com/Zewo/Venice"><img src="img/gh.png"/>View on GitHub</a></p>
      </div>
    </header>
    <div class="content-wrapper">
      <p id="breadcrumbs">
        <a href="index.html">Venice Reference</a>
        <img id="carat" src="img/carat.png" />
        Coroutines  Reference
      </p>
    </div>
    <div class="content-wrapper">
      <nav class="sidebar">
        <ul class="nav-groups">
          <li class="nav-group-name">
            <a href="Coroutines.html">Coroutines</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Classes/Coroutine.html">Coroutine</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Coroutine/Group.html">– Group</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Channels.html">Channels</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Classes/Channel.html">Channel</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Channel.html#/s:CC6Venice7Channel7Sending">– Sending</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Channel.html#/s:CC6Venice7Channel9Receiving">– Receiving</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="File Descriptors.html">File Descriptors</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Classes/FileDescriptor.html">FileDescriptor</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/FileDescriptor/PollEvent.html">– PollEvent</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Time.html">Time</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Structs/Duration.html">Duration</a>
              </li>
              <li class="nav-group-task">
                <a href="Structs/Deadline.html">Deadline</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/Int.html">Int</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Errors.html">Errors</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Enums/VeniceError.html">VeniceError</a>
              </li>
            </ul>
          </li>
        </ul>
      </nav>
      <article class="main-content">
        <section>
          <section class="section">
            <h1>Coroutines</h1>
            
          </section>
          <section class="section task-group-section">
            <div class="task-group">
              <ul>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:C6Venice9Coroutine"></a>
                    <a name="//apple_ref/swift/Class/Coroutine" class="dashAnchor"></a>
                    <a class="token" href="#/s:C6Venice9Coroutine">Coroutine</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Lightweight coroutine.</p>

<p>Launching coroutines and switching between them is extremely fast.
It requires only a few machine instructions.
This makes coroutines a suitable basic flow control mechanism,
like the <code>if</code> or <code>while</code> keywords, which have comparable performance.</p>

<p>Coroutines have one big limitation, though: All coroutines run on a single
CPU core. If you want to take advantage of multiple cores, you have to launch
multiple threads or processes, presumably as many of them as there are CPU cores
on your machine.</p>

<p>Coroutines are scheduled cooperatively. What that means is that a coroutine has to
explicitly yield control of the CPU to allow a different coroutine to run.
In a typical scenario, this is done transparently to the user: When a coroutine
invokes a function that would block (such as <code>Coroutine.wakeUp</code>, <code>FileDescriptor.poll</code>, <code>channel.send</code> or <code>channel.receive</code>),
the CPU is automatically yielded.
However, if a coroutine runs without calling any blocking functions, it may hold
the CPU forever. For these cases, the <code>Coroutine.yield</code> function can be used to manually relinquish
the CPU to other coroutines manually.</p>
<h2 id='example' class='heading'>Example:</h2>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">coroutine</span> <span class="o">=</span> <span class="k">try</span> <span class="kt">Coroutine</span> <span class="p">{</span>
    <span class="o">...</span>
<span class="p">}</span>

<span class="n">coroutine</span><span class="o">.</span><span class="nf">cancel</span><span class="p">()</span>
</code></pre>

                        <a href="Classes/Coroutine.html" class="slightly-smaller">See more</a>
                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight"><code><span class="kd">public</span> <span class="kd">final</span> <span class="kd">class</span> <span class="kt">Coroutine</span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
              </ul>
            </div>
          </section>
        </section>
        <section id="footer">
          <p>&copy; 2017 <a class="link" href="http://zewo.io" target="_blank" rel="external">Zewo</a>. All rights reserved. (Last updated: 2017-05-20)</p>
          <p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.8.1</a>, a <a class="link" href="http://realm.io" target="_blank" rel="external">Realm</a> project.</p>
        </section>
      </article>
    </div>
  </body>
</div>
</html>
